import {
  createRouter,
  createWebHistory,
  createWebHashHistory,
} from 'vue-router'
import type { RouteRecordRaw } from 'vue-router'
import HomeView from '../views/HomeView.vue'

const routes: Array<RouteRecordRaw> = [
  {
    path: '/home',
    // alias: ['/', '/root'], //可以给多个path指向同一个组件
    name: 'home',
    component: HomeView,
    meta: {
      title: 'home',
      transition: 'animate__fadeInUp',
    },
    // redirect:'home/user1',//1.传字符串path地址
    // redirect: {
    //   path: 'home/user1'    //2.传递一个对象
    //   // name:""
    // },
    // redirect: (to) => {
    //   //3.传回调函数
    //   console.log('to', to)
    //   return {
    //     path: 'home/user1',
    //     query: {
    //       msg: 'this is user 1',
    //     },
    //   }
    // },
    children: [
      {
        path: 'user1',
        components: {
          default: () => import('../views/A.vue'),
        },
        meta: {
          title: 'user1',
          transition: 'animate__bounceIn',
        },
      },
      {
        path: 'user2',
        components: {
          bbb: () => import('../views/B.vue'),
          ccc: () => import('../views/C.vue'),
        },
        meta: {
          title: 'user2',
        },
      },
    ],
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('@/views/Login.vue'),
    meta: {
      title: 'login',
      transition: 'animate__bounceIn',
    },
  },
]
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  scrollBehavior: (to, from, savePosition) => {
    console.log('savePosition', savePosition)
    if (savePosition) {//记住上次所在的位置
      return savePosition
    } else {
      top: 0
    }
  },
  routes,
})

export default router
